/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.db.model;

import com.db4o.config.annotations.Indexed;
import java.util.List;
import org.dafe.partyband.db.api.IAuthor;
import org.dafe.partyband.db.api.IBandMember;
import org.dafe.partyband.db.api.ISong;
import org.dafe.partyband.db.api.ISongResource;

/**
 *
 * @author dafe
 */
public class Song implements ISong {

    @Indexed
    private String name;
    private IAuthor author;
    private List<IBandMember> singers;
    private List<ISongResource> resources;

    public Song(String name, IAuthor author, List<IBandMember> singers, List<ISongResource> resources) {
        assert singers != null;
        assert resources != null;
        this.name = name;
        this.author = author;
        this.singers = singers;
        this.resources = resources;
        
        assureParentChain(resources);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public IAuthor getAuthor() {
        return author;
    }

    public void setAuthor(IAuthor author) {
        this.author = author;
    }

    public List<? extends IBandMember> getSingers() {
        return singers;
    }

    public void setSingers(List<IBandMember> singers) {
        this.singers = singers;
    }

    public List<ISongResource> getResources() {
        assureParentChain(resources);
        
        return resources;
    }

    public void addResource(ISongResource resource) {
        resources.add(resource);
        resource.setParent(this);
    }

    public void removeResource(ISongResource resource) {
        resources.remove(resource);
        resource.setParent(null);
    }

    public void addSinger(IBandMember singer) {
        singers.add(singer);
    }

    public void addSinger(int index, IBandMember singer) {
        singers.add(index, singer);
    }

    public void removeSinger(IBandMember singer) {
        singers.remove(singer);
    }
    
    private void assureParentChain(List<ISongResource> resources) {
        for (ISongResource songRes : resources) {
            songRes.setParent(this);
        }
    }

}
